home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / plot2d / stringkl.jav < prev   
Encoding:
Text File  |  1995-12-15  |  3.8 KB  |  149 lines

  1. import java.lang.*;
  2.  
  3. /*
  4. ** StringKludge
  5. **              A quick hack to convert float and double to
  6. ** a string to overcome a Major deficiency in Netscape 2.0b1
  7. **************************************************************************
  8. **    Copyright (C) 1995 Leigh Brookshaw
  9. **
  10. **    This program is free software; you can redistribute it and/or modify
  11. **    it under the terms of the GNU General Public License as published by
  12. **    the Free Software Foundation; either version 2 of the License, or
  13. **    (at your option) any later version.
  14. **
  15. **    This program is distributed in the hope that it will be useful,
  16. **    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. **    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. **    GNU General Public License for more details.
  19. **
  20. **    You should have received a copy of the GNU General Public License
  21. **    along with this program; if not, write to the Free Software
  22. **    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. **************************************************************************
  24. */
  25.  
  26.  
  27. class StringKludge extends Object {
  28.  
  29. /*
  30. **   Maximum number of digits allowed in the ordinal before we go to
  31. **   scientific notation
  32. */
  33.      public int MaxOrdinal = 4;
  34. /*
  35. **   Number of digits in the fraction before we round up.
  36. */
  37.      public int MaxFraction = 4;   
  38.  
  39.  
  40.  
  41.      StringKludge() {
  42.            super();
  43.      }
  44.  
  45.      StringKludge(int o, int f) {
  46.           MaxOrdinal  = o;
  47.           MaxFraction = f;
  48.      }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.      public String valueOf(float f) {
  55.         return valueOf( (double)f );
  56.      }
  57.  
  58.      public String valueOf(double d) {
  59.  
  60.           int i;
  61.           double fraction;
  62.           double v = d;
  63.           int ordinal  = 0;
  64.           int exponent = 0;
  65.           String s;
  66.           StringBuffer sb = new StringBuffer();
  67.  
  68.           if(v != 0.0) exponent = (int)(Math.floor( log10(Math.abs(v)) ) );
  69.  
  70.           if(exponent > 0 && exponent <= MaxOrdinal ) {
  71.                exponent = 0;
  72.           } else
  73.           if(exponent < 0 && Math.abs(exponent) <= MaxFraction ) {
  74.                exponent = 0;
  75.           } else {
  76.              if( exponent < 0 ) {
  77.                 for(i=exponent; i<0; i++) { v *= 10.0; }
  78.              } else {
  79.                 for(i=0; i<exponent; i++) { v /= 10.0; }
  80.              }
  81.           }
  82.  
  83.           if(v < 0 ) v -= 0.5*Math.pow(10,-MaxFraction);
  84.           else       v += 0.5*Math.pow(10,-MaxFraction);
  85.           ordinal  = (int)v;
  86.           fraction = Math.abs(v - ordinal);
  87.  
  88.           sb.append(ordinal);
  89.           if(fraction != 0.0 ) {
  90.                s = stringfraction(fraction);
  91.                if( s != null) sb.append(s);
  92.           } 
  93.           if(exponent != 0 )  {
  94.                  s = stringexponent(exponent);
  95.                  if( s != null) sb.append(s); 
  96.           }
  97.           return sb.toString();
  98.  
  99.  
  100.      }
  101.     
  102.  
  103.      private String stringfraction(double f) {
  104.            double d = f;
  105.            int j;
  106.            int i;
  107.            int k;
  108.            double c;
  109.  
  110.            d *= Math.pow(10,MaxFraction);
  111.            i = (int)d;
  112.  
  113.            if(i == 0) return null;
  114.  
  115. //         Strip trailing zeros
  116.            for(j=0;j<MaxFraction;j++) {
  117.                k = ( (int)(i/10) )*10;
  118.                if( i == k ) i /= 10;
  119.                else break;
  120.            }
  121.            
  122.            if(i == 0) return null;
  123.  
  124.  
  125.            return ".".concat(String.valueOf(i));
  126.  
  127.      }
  128.  
  129.      private String stringexponent(int exponent) {
  130.           StringBuffer s = new StringBuffer("e");
  131.           s.append(String.valueOf(exponent));
  132.           return s.toString();
  133.      }
  134.  
  135.  
  136.  
  137.  
  138.       private double log10( double a ) throws ArithmeticException {
  139.            return Math.log(a)/2.30258509299404568401;
  140.       }
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. }
  149.